package com.jimmy.medical.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CommonUtils {
    private static String shortTimeFormat = "yyyyMMddHHmmss";
    private static String normalTimeFormat = "yyyy-MM-dd HH:mm:ss";
    private static String shortDateFormat = "yyyyMMdd";
    private static String normalDateFormat = "yyyy-MM-dd";

    public static String formatDate(String format, Date date) {
        SimpleDateFormat sf = new SimpleDateFormat(format);
        return sf.format(date);
    }

    public static Date convertToDate(String str, String format) {
        SimpleDateFormat sf = new SimpleDateFormat(format);
        Date date = new Date();
        try {
            date = sf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static Date getTodayTimeZero() {
        String str = getNowDateShortString();
        return convertToDate(str, shortDateFormat);
    }

    public static Date getFirstDayOfMonth() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DATE, 1);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        return c.getTime();
    }

    public static String getFirstDayOfMonthStr() {
        Date date = getFirstDayOfMonth();
        return formatDate(shortTimeFormat, date);
    }

    public static String getNowTimeShortString() {
        return formatDate(shortTimeFormat, new Date());
    }

    public static String getNowTimeNormalString() {
        return formatDate(normalTimeFormat, new Date());
    }

    public static String getNowDateShortString() {
        return formatDate(shortDateFormat, new Date());
    }

    public static String getNowDateNormalString() {
        return formatDate(normalDateFormat, new Date());
    }

    public static String convertAsShortTimeFormat(String origin) {
        return origin == null ? origin : origin.replaceAll("[-|:|\\s|年|月|日|时|分|秒|/]", "");
    }
}
